package com.wifi.game.servlet;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wifi.annotation.ShenZhouWifiMethod;
import com.wifi.base.BaseServlet;
import com.wifi.baseEntity.BaseJSON;
import com.wifi.game.entity.GameEntity;
import com.wifi.game.service.GameService;
import com.wifi.game.service.GameServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/game/*")

public class GameServlet_  extends BaseServlet {
         GameService gameService=new GameServiceImp();


    @ShenZhouWifiMethod("/insert")
    public  void  insert(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String name = request.getParameter("name");
        String title = request.getParameter("title");
       Integer downloadCount = Integer.valueOf(request.getParameter("downloadCount")) ;
        float size = Float.valueOf(request.getParameter("size")) ;
        String des = request.getParameter("des");
        String gameIcon = request.getParameter("gameIcon");
        String iosUrl = request.getParameter("iosUrl");
        String androidUrl = request.getParameter("androidUrl");
        String pictures = request.getParameter("pictures");
      Integer status = Integer.valueOf(request.getParameter("status") );
        String type = request.getParameter("type");
        Integer recType = Integer.valueOf(request.getParameter("recType"));
        Integer platform = Integer.valueOf(request.getParameter("platform")) ;
        //  上面格式已经转换 完毕

        GameEntity gameEntity = new GameEntity(name,title,downloadCount,size,des,gameIcon,iosUrl,androidUrl,pictures,status
        ,type,recType,platform);
        gameService.insert(gameEntity);
        request.getRequestDispatcher("/WEB-INF/game/list/gameList.jsp").forward(request,response);

    }

    @ShenZhouWifiMethod("/add")  //仅跳转
    public  void  add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        System.out.println("马上跳转到add 的jsp");
       request.getRequestDispatcher("/WEB-INF/game/add/gameAdd.jsp").forward(request,response);

    }


    @ShenZhouWifiMethod("/edit")  //仅跳转
    public  void  edit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String gidstr = request.getParameter("gid");
        Integer gid = Integer.valueOf(gidstr);
        request.getSession().setAttribute("gid",gid);
        request.getRequestDispatcher("/WEB-INF/game/edit/gameEdit.jsp").forward(request,response);

    }
    @ShenZhouWifiMethod("/doEdit")  //获取数值
    public  void  doEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String gidstr = request.getParameter("gid");
        Integer gid= Integer.valueOf(gidstr);
        Object o = gameService.find(gid);
        System.out.println("已经获取值了，马上输出"+o);
       response.getWriter().write(JSON.toJSONString(o));
        System.out.println("jiushiwan");


    }


    @ShenZhouWifiMethod("/doEditing")  // 修改 的时候输入新的数值
    public  void  doEditing(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        Object gid1 = request.getSession().getAttribute("gid");
        int gid = (int) gid1;

        String name = request.getParameter("name");
        String title = request.getParameter("title");
        Integer downloadCount = Integer.valueOf(request.getParameter("downloadCount")) ;
        float size = Float.valueOf(request.getParameter("size")) ;
        String des = request.getParameter("des");
        String gameIcon = request.getParameter("gameIcon");
        String iosUrl = request.getParameter("iosUrl");
        String androidUrl = request.getParameter("androidUrl");
        String pictures = request.getParameter("pictures");
        Integer status = Integer.valueOf(request.getParameter("status") );
        String type = request.getParameter("type");
        Integer recType = Integer.valueOf(request.getParameter("recType"));
        Integer platform = Integer.valueOf(request.getParameter("platform")) ;
        //  上面格式已经转换 完毕

        GameEntity gameEntity = new GameEntity(name,title,downloadCount,size,des,gameIcon,iosUrl,androidUrl,pictures,status
                ,type,recType,platform);
        gameService.edit(gid,gameEntity);
        response.sendRedirect(request.getContextPath()+"/game/find");

//        System.out.println(o);
//        response.getWriter().write(JSON.toJSONString(o));

    }

    @ShenZhouWifiMethod("/remove")
    public  void  remove(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String gid = request.getParameter("gid");
        Integer s=  Integer.valueOf(gid);
           gameService.remove(s);
        response.sendRedirect(request.getContextPath()+"/game/find");

    }

}
